package cn.edu.scau.cmi.chenxiaoqin.client;

import cn.edu.scau.cmi.chenxiaoqin.singleton.Chairman;
import cn.edu.scau.cmi.chenxiaoqin.singleton.Marshal;
import javafx.application.Application;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SingletonClient extends Application{
	public int count=0;
	public void start(Stage primaryStage) {
		//大布局
		BorderPane pane =new BorderPane();
		VBox vBox=new VBox();
		vBox.setPadding(new Insets(15,15,15,15));
		//下拉框
		ComboBox<String> modelComboBox = new ComboBox<String>();
		modelComboBox.getItems().addAll( "单例模式","多例模式" );   
		modelComboBox.setValue("单例模式");
		modelComboBox.setOnAction(e->{
			vBox.getChildren().clear();
			count=0;
		});
		//输入框
        TextField inputName=new TextField("在此输入主席名字");
        //按钮
		Button createButton=new Button("创建");
		createButton.setOnAction(e->{
			String model=modelComboBox.getValue();
			String name=inputName.getText();
			if(model.equals("单例模式")) {
				Chairman a=Chairman.getInstanece(name);
				Label lb1=new Label(a.getName()+a);
				vBox.getChildren().add(lb1);
			}
			else {
				if(count>=10) {
					Label lb2=new Label("您已经创建了10个元帅，不能再创建了！");
					vBox.getChildren().add(lb2);
				}
				else {
					vBox.getChildren().clear();
					Marshal[] b=Marshal.getInstanece(name);
					count=count+1;
					Label[] lb2 = new Label[10];
					System.out.println(count);
					for(int i=0;i<count;i++) {
						lb2[i]=new Label(b[i].getName()+b[i]);
						vBox.getChildren().add(lb2[i]);
					}
				}
			}
		});
		//上面的操作区域
		HBox  hBox=new HBox(15);
		hBox.setPadding(new Insets(15,15,15,15));
		hBox.getChildren().add(modelComboBox);
		hBox.getChildren().add(inputName);
		hBox.getChildren().add(createButton);
		pane.setTop(hBox);
		pane.setCenter(vBox);
		Scene scene =new Scene(pane,700,400);
		primaryStage.setTitle("FX");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Application.launch(args);
	}
}
